﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rose.Models;


namespace Rose.Helper
{
    public class CommentHelpModel
    {
        private  Database_RoseEntities db = new Database_RoseEntities();

        #region FeelingComment
        private  bool UpdateFeelingCommentCount(string feelingId)
        {
            try
            {
                Feeling model = db.Feelings.Where(c => c.Id == new Guid(feelingId)).FirstOrDefault();
                if (model == null)
                    return false;
                model.CommentCount += 1;
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public  CommentModel AddFeelingCommentToDatabase(string selfId, string feelingId, string content)
        {
            try
            {
                UserInfo selfInfo = new CheckHelpModel().CheckUserInfo(selfId);
                string imagePath = selfInfo.ImagePath;
                if (UpdateFeelingCommentCount(feelingId))
                {
                    FeelingComment comment = new FeelingComment()
                    {
                        Id = Guid.NewGuid(),
                        CreateTime = DateTime.Now,
                        UserId = selfId,
                        Content = CheckDataHelpModel.CheckLength(HttpContext.Current.Server.UrlDecode(content), 144),
                        FeelingId = new Guid(feelingId)

                    };
                    db.AddToFeelingComments(comment);
                    db.SaveChanges();
                    return new FeelingCommentHelpModel().ChangeCommentToCommentModel(comment);
                }
                return new CommentModel();

            }
            catch
            {
                return new CommentModel();
            }
        }

        public  bool RemoveFeelingCommentFromDatabase(string id,string selfId)
        {
            try
            {
                Guid guidId = new Guid(id);
                FeelingComment comment = db.FeelingComments.Where(c => c.Id == guidId && c.UserId==selfId).FirstOrDefault();
                if (comment == null)
                    return true;
                db.FeelingComments.DeleteObject(comment);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion  


        #region ArtcleComment

        private  bool UpdateArticleCommentCount(string articleId)
        {
            try
            {
                Article model = db.Articles.Where(c => c.Id == new Guid(articleId)).FirstOrDefault();
                if (model == null)
                    return false;
                model.CommentCount += 1;
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public  CommentModel AddArticleCommentToDatabase(string selfId, string articleId, string content)
        {
            try
            {
                UserInfo selfInfo =new  CheckHelpModel().CheckUserInfo(selfId);
                string imagePath = selfInfo.ImagePath;
                if (UpdateArticleCommentCount(articleId))
                {
                    ArticleComment comment = new ArticleComment()
                    {
                        Id = Guid.NewGuid(),
                        CreateTime = DateTime.Now,
                        UserId = selfId,
                        Content = CheckDataHelpModel.CheckLength(HttpContext.Current.Server.UrlDecode(content), 144),
                        ArticleId = new Guid(articleId)

                    };
                    db.AddToArticleComments(comment);
                    db.SaveChanges();
                    return new ArticleCommentHelpModel().ChangeCommentToCommentModel(comment);
                }
                return new CommentModel();

            }
            catch
            {
                return new CommentModel();
            }
        }

        public  bool RemoveArticleCommentFromDatabase(string id,string selfId)
        {
            try
            {
                Guid guidId = new Guid(id);
                ArticleComment comment = db.ArticleComments.Where(c => c.Id == guidId && c.UserId==selfId).FirstOrDefault();
                if (comment == null)
                    return true;
                db.ArticleComments.DeleteObject(comment);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        #endregion


     



    }
}